import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import findLast from "lodash/findLast";
import { notification } from "ant-design-vue";
import NotFound from "../views/404.vue";
import ForBidden from "../views/403.vue";
import { check, isLogin, getCurrentAuthority } from "../utils/auth";

Vue.use(VueRouter);

const routes = [
  {
    path: "/home/examination/starting",
    hideInMenu: true,
    meta: {
      title: "开始考试",
      authority: ["student", "admin"],
      requireAuth: true
    },
    name: "examination",
    component: () =>
      import(
        /* webpackChunkName: "home" */ "../views/Dashboard/Student/Examination.vue"
      )
  },
  {
    path: "/home/check/examinationPaper",
    hideInMenu: true,
    meta: {
      icon: "edit",
      title: "查看试卷",
      authority: ["student", "teacher", "admin"],
      requireAuth: true
    },
    name: "checkExaminationPaper",
    component: () =>
      import(
        /* webpackChunkName: "home" */ "../views/Dashboard/Student/CheckExamPaper.vue"
      )
  },
  {
    path: "/",
    hideInMenu: true,
    component: () =>
      import(/* webpackChunkName: "index" */ "../views/Index.vue")
  },
  {
    path: "/Login",
    hideInMenu: true,
    name: "login",
    component: () =>
      import(/* webpackChunkName: "Login" */ "../views/User/Login.vue")
  },
  {
    path: "/Register",
    name: "register",
    hideInMenu: true,
    component: () =>
      import(/* webpackChunkName: "Register" */ "../views/User/Register.vue")
  },
  {
    path: "/home",
    meta: {
      authority: ["student", "teacher", "admin"],
      requireAuth: true
    },
    component: () =>
      import(/* webpackChunkName: "home" */ "../layouts/BasicLayout.vue"),
    children: [
      {
        path: "/home/dashboard",
        meta: {
          icon: "dashboard",
          title: "仪表盘",
          authority: ["student", "admin"],
          requireAuth: true
        },
        name: "dashboard",
        component: () =>
          import(
            /* webpackChunkName: "home" */ "../views/Dashboard/Student/Home.vue"
          )
      },
      {
        path: "/home/grade",
        meta: {
          icon: "area-chart",
          title: "我的成绩",
          authority: ["student", "admin"],
          requireAuth: true
        },
        name: "grade",
        component: () =>
          import(
            /* webpackChunkName: "home" */ "../views/Dashboard/Student/Grade.vue"
          )
      },
      {
        path: "/home/examination",
        meta: {
          icon: "edit",
          title: "我的考试",
          authority: ["student", "admin"],
          requireAuth: true
        },
        name: "examination",
        component: () =>
          import(
            /* webpackChunkName: "home" */ "../views/Dashboard/Student/Exam.vue"
          )
      },

      {
        path: "/home/studentData/",
        meta: {
          icon: "bar-chart",
          title: "数据总览",
          authority: ["teacher", "admin"],
          requireAuth: true
        },
        name: "studentData",
        component: { render: h => h("router-view") },
        children: [
          {
            path: "/home/studentData/analysis",
            meta: { title: "所有数据总览", requireAuth: true },
            name: "analysisStudent",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/StudentData/dataCheck.vue"
              )
          },
          {
            path: "/home/studentData/examinationTotal",
            meta: { title: "学生考试统计", requireAuth: true },
            name: "examinationTotal",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/StudentData/testStatistics.vue"
              )
          }
        ]
      },
      {
        path: "/home/class",
        meta: {
          icon: "compass",
          title: "班级管理",
          authority: ["teacher", "admin"],
          requireAuth: true
        },
        name: "class",
        // hideChildrenMenu: true,
        component: { render: h => h("router-view") },
        children: [
          {
            path: "/home/class/create",
            meta: { title: "创建班级", requireAuth: true },
            hideInMenu: true,
            name: "createClass",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Class/myClass.vue"
              )
          },
          {
            path: "/home/class/clbum",
            meta: { title: "我的班级", requireAuth: true },
            name: "myClass",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Class/myClass.vue"
              )
          }
        ]
      },
      {
        path: "/home/exam",
        meta: {
          icon: "form",
          title: "考试管理",
          authority: ["teacher", "admin"],
          requireAuth: true
        },
        name: "exam",
        component: { render: h => h("router-view") },
        children: [
          {
            path: "/home/exam/questionManagement",
            meta: { title: "题库管理", requireAuth: true },
            name: "questionManagement",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Questions/QuestionManagement.vue"
              )
          },
          // {
          //   path: "/home/exam/questionManagement/",
          //   meta: { title: "题库详细管理" },
          //   name: "questionDetailManagement",
          //   props: true,
          //   hideInMenu:true,
          //   component: () =>
          //     import(
          //   /* webpackChunkName: "home" */ "../views/Dashboard/Questions/QuestionBank.vue"
          //     )
          // },
          {
            path: "/home/exam/examinationPaperManagement",
            meta: { title: "试卷管理", requireAuth: true },
            name: "examinationPaperManagement",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Questions/ExaminationPaperManaged.vue"
              )
          },
          {
            path: "/home/exam/examinationPaperCreated",
            meta: { title: "试卷创建", requireAuth: true },
            name: "examinationPaperCreated",
            hideInMenu: true,
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Questions/ExaminationPaperCreated.vue"
              )
          },
          {
            path: "/home/exam/examinationManagement",
            meta: { title: "考试管理", requireAuth: true },
            name: "examinationManagement",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Questions/ExaminationManaged.vue"
              )
          },
          {
            path: "/home/exam/examinationCreated",
            meta: { title: "考试创建", requireAuth: true },
            name: "examinationCreated",
            hideInMenu: true,
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Questions/ExaminationCreated.vue"
              )
          },
          {
            path: "/home/exam/markingManagement",
            meta: { title: "阅卷管理", requireAuth: true },
            name: "markingManagement",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Questions/Marking.vue"
              )
          }
        ]
      },
      {
        path: "/home/notice",
        component: { render: h => h("router-view") },
        meta: {
          icon: "bell",
          title: "消息通知",
          authority: ["student", "teacher", "admin"],
          requireAuth: true
        },
        name: "notice",
        children: [
          {
            path: "/home/notice/check",
            meta: { title: "我的通知", requireAuth: true },
            name: "checkNotice",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Notice.vue"
              )
          }
        ]
      },
      {
        path: "/home/account",
        component: { render: h => h("router-view") },
        meta: {
          icon: "user",
          title: "账户管理",
          authority: ["student", "teacher", "admin"],
          requireAuth: true
        },
        name: "account",
        children: [
          {
            path: "/home/account/info",
            meta: {
              title: "个人中心",
              authority: ["admin"],
              requireAuth: true
            },
            name: "modifyInfo",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Account/Info.vue"
              )
          },
          {
            path: "/home/account/setting",
            meta: { title: "个人设置", requireAuth: true },
            name: "setting",
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Dashboard/Account/Setting.vue"
              )
          }
        ]
      },
      {
        path: "/home/form",
        hideInMenu: true,
        component: { render: h => h("router-view") },
        meta: {
          icon: "user",
          title: "表单信息",
          authority: ["student", "teacher", "admin"],
          requireAuth: true
        },
        name: "form",
        children: [
          {
            path: "/home/form/step1",
            name: "step1",
            meta: { requireAuth: true },
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Forms/StepForm/Step1.vue"
              )
          },
          {
            path: "/form/step-form/confirm",
            name: "step2",
            meta: { requireAuth: true },
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Forms/StepForm/Step2.vue"
              )
          },
          {
            path: "/form/step-form/result",
            name: "step3",
            meta: { requireAuth: true },
            component: () =>
              import(
                /* webpackChunkName: "home" */ "../views/Forms/StepForm/Step3.vue"
              )
          }
        ]
      }
    ]
  },
  {
    path: "*",
    name: "404",
    hideInMenu: true,
    component: NotFound
  },
  {
    path: "/403",
    name: "403",
    hideInMenu: true,
    component: ForBidden
  }
];

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  console.log("to");
  console.log(to);
  console.log("from");
  console.log(from);

  if (to.path !== from) {
    NProgress.start();
  }

  if (
    to.path == "/home" ||
    to.path == "/Home" ||
    to.path == "/home/" ||
    to.path == "/Home/"
  ) {
    // 判断是学生访问还是教师访问 跳转不同的首页
    const author = getCurrentAuthority();
    // 学生访问
    if (author.includes("student") || author.includes("admin")) {
      next({ path: "/home/dashboard" });
    } else if (author.includes("teacher") || author.includes("admin")) {
      next({ path: "/home/studentData/analysis" });
    }
    console.log(author.includes("teacher"));
  }

  // 判断权限
  const record = findLast(to.matched, record => record.meta.authority);
  if (record && !check(record.meta.authority)) {
    if (!isLogin() && to.path !== "/user/login") {
      next({ path: "/user/login" });
    } else if (to.path !== "/403") {
      notification.error({
        message: "403 Forbidden",
        description: "You do not have permission to access!"
      });
      next({ path: "/403" });
    }
    NProgress.done();
  }
  next();
});

router.afterEach(() => {
  NProgress.done();
});
export default router;
